<?
var_dump(setlocale(LC_ALL,'en_AU.utf8'));

ini_set('include_path', '/home/tadams/classes/');
require_once('definedValues/DataMediator.php');

$request= new DataMediator($_REQUEST);

// DATE field
$request->setConverter('start','Date')
	->setDefault(new DateTime())
	->setStyle('%d/%m/%Y')
	->setErrorMessage('Your input "%s" is not a date.');
$request->addValidator('start','Required')
	->setErrorMessage('A date must be entered');
$date= new DateTime('next week');
$dateRange= $request->addValidator('start','DateRange')
	->setMin($date)
	->setErrorMessage(
		'Your date must be after '.
		ConverterBase::forName('Date')->setStyle('%x')->getAsString($date)
	);

// ENABLED field
$request->setConverter('enabled','Array')
	->setErrorMessage('Your input "%s" is not a list.');
$request->addValidator('enabled','ArrayRange')
	->setMin(1)
	->setMax(2)
	->setErrorMessage('Please select one but not more than two options');

// FORM submitted
if ($_REQUEST['submitted']) {
	if (!($errors= $request->getErrorMessages())) {
		?>
		<h2>Submitted values</h2>
		<?
		var_dump($request->getAsDatatype('enabled'));
		echo(
			ConverterBase::forName('Date')
				->setStyle('%c')
				->getAsString($request->getAsDatatype('start'))
		);
	}
}

// ERROR output
if (!empty($errors)) {
	echo('<ul>');
	foreach ($errors as $error) {
		$msg= $error->getErrorMessage();
		if ($msg) echo('<li>'.$msg.'</li>');
	}
	echo('</ul>');
}

// INPUT mask
?>
<h2>Please enter following fields</h2>
<form>
	<input type="hidden" name="submitted" value="true" />
	<span>Date:</span>
	<input type="text" name="start" value="<?= $request->getAsString('start'); ?>" />
	<?
	if ($errors= $request->getErrorMessages('start')) {
		foreach ($errors as $error) {
			if ($error->isValidator($dateRange)) {
				?>
				<span>
					Please enter a date starting earliest
					<?=
					ConverterBase::forName('Date')
						->setStyle('%c')
						->getAsString($dateRange->getMin());
					?>
				</span>
				<?
			}
		}
	}
	?>
	<br/>
	<?
	$selected= $request->getAsDatatype('enabled');
	$selected= $selected ? $selected : array();
	?>
	<input
		type="checkbox" name="enabled[]" value="1"
		<?= in_array('1',$selected) ? 'checked' : ''; ?>
	/> Enable 1<br/>
	<input
		type="checkbox" name="enabled[]" value="2"
		<?= in_array('2',$selected) ? 'checked' : ''; ?>
	/> Enable 2<br/>
	<input
		type="checkbox" name="enabled[]" value="3"
		<?= in_array('3',$selected) ? 'checked' : ''; ?>
	/> Enable 3<br/>
	
	<input type="submit" value="Submit" />
</form>
